<?php

/**
 * Classe Post: gestisce i topic e le sue informazioni, un topic può avere più post e un post è legato ad un solo topic
 *
 * @author Stefano Poncini
 */
class Post {
    
    public $id_post;
    public $content;
    public $creator;
    public $quote_id;
    public $creation_date;
    public $updated;
    public $update_date;
    public $topic_id;
    
    /**
     * Costruttore della classe Topic: crea una nuova istanza e valorizza le variabili globali
     * 
     * @param int $id_post
     * @param string $content
     * @param string $creator
     * @param date $creation_date
     * @param int $topic_id
     */
    function __construct($id_post, $content, $creator, $creation_date, $topic_id) {
        $this->id_post = $id_post;
        $this->content = $content;
        $this->creator = $creator;
        $this->creation_date = $creation_date;
        $this->topic_id = $topic_id;
    }
    
    /**
     * Metodo insertPost: crea un nuovo post (record) sul DB
     * 
     * @param string $content
     * @param string $creator
     * @param date $date
     * @param int $id_topic
     * @return boolean
     */
    public static function insertPost($content, $creator, $date, $id_topic){
        try {
            $sql = "INSERT INTO post(`id_post`,`content`,`creator`,`creation_date`,`topic_id`,`visible`) VALUES(?,?,?,?,?,?)";

            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array(null, htmlentities(Util::parseToDB($content)), $creator, $date, $id_topic, 1));
            $id = DB::getDBO()->lastInsertId();
            return $id;
        } catch (PDOException $e) {

            return false;
        }
    }
    
    /**
     * Metodo deletePost: "elimina" un post, impostando il campo "visible" a 0
     * 
     * @param type $id
     * @return boolean
     */
    public static function deletePost($id){
        
        try {
            $sql = "UPDATE post SET visible = ? WHERE id_post = ?";
            $stmt = DB::getDBO()->prepare($sql);
            $stmt->execute(array(0,$id));
            return true;
        } catch (PDOException $e) {

            return false;
        }
    }

}

?>
